package longrise.phone.com.bjjt_jyb.bindinginformation;

import android.content.Context;
import android.graphics.Color;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.FormParameter;
import com.longrise.android.LFView;
import com.longrise.android.loaddata.newloaddata.LoadDataManagerCar;
import com.longrise.android.loaddata.newloaddata.LoadDataManagerFather;

import java.util.ArrayList;
import java.util.List;

import longrise.phone.com.bjjt_jyb.Adapter.MyCarAdapter;
import longrise.phone.com.bjjt_jyb.Application.BaseApplication;
import longrise.phone.com.bjjt_jyb.Constant.URLConstant;
import longrise.phone.com.bjjt_jyb.R;
import longrise.phone.com.bjjt_jyb.Utils.DialogUtils;
import longrise.phone.com.bjjt_jyb.Utils.UiUtil;
import longrise.phone.com.bjjt_jyb.Utils.UiUtils;


/**
 * 我的推荐详情
 */
public class MyCarView extends LFView implements SwipeRefreshLayout.OnRefreshListener, BaseQuickAdapter.OnRecyclerViewItemClickListener, BaseQuickAdapter.RequestLoadMoreListener {
    private Context context = null;
    private View view = null;
    private EntityBean[] beans;
    private MyCarAdapter adapter;
    private BaseApplication application;
    private EntityBean[] beanLists;
    private List<EntityBean> beanArrayList = new ArrayList<>();
    private ImageView empty_listview;
    private int pageNum = 1;

    private int pageSize = 20;
    private int number;
    private SwipeRefreshLayout sf_mycar;
    private RecyclerView rv_mycar;

    public MyCarView(Context c, EntityBean[] bean, int number) {
        super(c);
        this.context = c;
        this.beans = bean;
        this.number = number;
    }

    @Override
    public void refreshByTime() {

    }

    @Override
    public FormParameter getFormParameter() {
        FormParameter fp = new FormParameter();
        fp.setShowtitle(false);
        return fp;
    }

    @Override
    public View getView() {
        return view;
    }

    @Override
    public void init() {
        initView();
    }

    private void initView() {
        view = View.inflate(context, R.layout.view_mycar, null);
        application = (BaseApplication) context.getApplicationContext();
        sf_mycar = (SwipeRefreshLayout) view.findViewById(R.id.sf_mycar);
        rv_mycar = (RecyclerView) view.findViewById(R.id.rv_mycar);
        empty_listview = (ImageView) view.findViewById(R.id.empty_listview);
        initSwipeRefreshLayoutAndRecyclerView();
        //标题栏
        TextView titleTV = (TextView) view.findViewById(R.id.tv_title);
        titleTV.setText("我的推荐");
        view.findViewById(R.id.hn_btn_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                OnDestroy();
                closeForm();
            }
        });
        for (EntityBean e : beans) {
            beanArrayList.add(e);
        }
    }

    private void initSwipeRefreshLayoutAndRecyclerView() {
        sf_mycar = (SwipeRefreshLayout) view.findViewById(R.id.sf_mycar);
        sf_mycar.setOnRefreshListener(this);
        sf_mycar.setColorSchemeColors(Color.rgb(47, 223, 189));
        rv_mycar = (RecyclerView) view.findViewById(R.id.rv_mycar);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rv_mycar.setLayoutManager(layoutManager);
        adapter = new MyCarAdapter(context, beanArrayList);
        adapter.setOnLoadMoreListener(this);
        adapter.openLoadMore(20, true);
        adapter.setOnRecyclerViewItemClickListener(this);
        rv_mycar.setAdapter(adapter);
    }

    @Override
    public void refresh() {
        DialogUtils.showDialog(context);
        beanArrayList.clear();
        loadData(0);
    }

    @Override
    public void OnDestroy() {
        super.OnDestroy();
        view = null;
        DialogUtils.destroyDialog();
    }

    //加载数据
    private void loadData(final int fromHeadorBootom) {
        EntityBean bean = new EntityBean();
        bean.set("userflag", application.getKckpName());
        bean.set("token", application.getToken());
        bean.set("pagenum", pageNum + "");
        bean.set("pagesize", pageSize + "");
        LoadDataManagerCar.getInstance(context).callService(null, application.getServerUrl(), URLConstant.JJAPPGETMYRECOMMEND, bean, new LoadDataManagerFather.OnRequestCompleteListener() {
            @Override
            public void onSuccess(String key, String service, Object result) {
                if (null != sf_mycar && sf_mycar.isRefreshing()) {// 停止刷新
                    sf_mycar.setRefreshing(false);
                }
                DialogUtils.closeDialog();
                beanLists = null;//重置数据
                if (fromHeadorBootom == 0) {
                    beanArrayList.clear();
                }
                EntityBean bean = (EntityBean) result;
                String restate = bean.getString("restate");
                if (TextUtils.equals("1", restate)) {
                    EntityBean entityBean = bean.getBean("data");
                    if (null != entityBean) {
                        beanLists = entityBean.getBeans("carlist");
                    }
                    if (null != beanLists) {
                        for (EntityBean e : beanLists) {
                            beanArrayList.add(e);
                        }
                        pageNum++;
                    }
                }
                if (null == beanLists || beanLists.length <= 0) {
                    empty_listview.setVisibility(View.VISIBLE);
                    adapter.setEmptyView(empty_listview);
                } else {
                    empty_listview.setVisibility(View.GONE);//null
                }
                if (null != adapter) {
                    adapter.notifyDataSetChanged();
                    if (beanArrayList.size() == number) {
                        adapter.notifyDataChangedAfterLoadMore(false);
                        UiUtil.showToast(context,"已经加载完毕");
                    }
                }
            }

            @Override
            public void onError(String key, String service, LoadDataManagerFather.ResultType resultType) {
                if (null != sf_mycar && sf_mycar.isRefreshing()) {// 停止刷新
                    sf_mycar.setRefreshing(false);
                }
                DialogUtils.closeDialog();
                UiUtil.showToast(context, "连接超时，请检查网络后重试。");
            }

            @Override
            public void onFinished(String key, String service) {
                DialogUtils.closeDialog();
            }
        });
    }

    @Override
    public void onRefresh() {
        pageNum = 1;
        adapter.notifyDataChangedAfterLoadMore(true);
        loadData(0);
    }

    @Override
    public void onItemClick(View view, int i) {

    }

    @Override
    public void onLoadMoreRequested() {
        new Handler().postDelayed(new Runnable() {
            public void run() {
                if (beanArrayList.size() < number) {
                    //还有更多数据
                    loadData(1);
                } else {
                    adapter.notifyDataChangedAfterLoadMore(false);
                }
            }

        }, 2000);
    }
}